package com.crm.base.service;

import com.crm.common.mapper.ProductMapper;
import com.crm.common.pojo.Product;
import com.crm.mybatis.separation.Read;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class ProductService {
    
    @Autowired
    private ProductMapper productMapper;

    /**
     * 查询所有
     *  分页查询
     * @param page  页码
     * @param size  页面大小
     * @param condition 条件
     * @return
     */
    @Read
    public PageInfo<Product> findAll(int page, int size, Product condition) {
        PageHelper.startPage(page,size);

        condition = condition != null ? condition : new Product();

        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();

        if (StringUtils.isNoneBlank(condition.getName())) {
            criteria.orLike("name","%" + condition.getName() + "%");
        } else if (StringUtils.isNotBlank(condition.getType())){
            criteria.orLike("type","%" + condition.getType() + "%");
        } else if (StringUtils.isNotBlank(condition.getBatch())) {
            criteria.orLike("batch","%" + condition.getBatch() + "%");
        }

        List<Product> products = productMapper.selectByExample(example);
        return new PageInfo<>(products);
    }
}